﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Library.Connection;
using Library.Entity;
using System.Data.SqlClient;
using Library.SQL;
using System.Data;
using MySql.Data.MySqlClient;

namespace Library.Services
{
    public class BookStockService
    {
        DataBaseConnectivity m_dataBaseConectivity = new DataBaseConnectivity();
        public BookStockService()
        { }
        internal BookStock GetBookStockByBookId(int BookId)
        {
            BookStock result = null;
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.SelectBookStockByBookId(), m_dataBaseConectivity.MySqlConnection);
                cmd.Parameters.Add("BookId", MySqlDbType.Int32).Value = BookId;
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result = new BookStock(rd);
                }
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
            return result;
        }
        internal void InsertNewBookStock(BookStock BookStock)
        {
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.InsertBookStock(), m_dataBaseConectivity.MySqlConnection);

                cmd.Parameters.Add("Code", MySqlDbType.VarChar).Value = BookStock.Code;
                cmd.Parameters.Add("Name", MySqlDbType.VarChar).Value = BookStock.Name;
                cmd.Parameters.Add("Remark", MySqlDbType.VarChar).Value = BookStock.Remark;
                cmd.Parameters.Add("CreatedDate", MySqlDbType.Datetime).Value = DateTime.Now;
                cmd.Parameters.Add("ModifiedDate", MySqlDbType.Datetime).Value = DateTime.Now;
                cmd.Parameters.Add("CreatedUserId", MySqlDbType.Int32).Value = BookStock.CreatedUserId;
                cmd.Parameters.Add("ModifiedUserId", MySqlDbType.Int32).Value = BookStock.ModifiedUserId;
                cmd.Parameters.Add("BookId", MySqlDbType.Int32).Value = BookStock.BookId;
                cmd.Parameters.Add("BookBalance", MySqlDbType.Decimal).Value = BookStock.BookBalance;
                cmd.ExecuteNonQuery();
                cmd.CommandText = SQLScript.SelectMaxBookStockId();
                cmd.Parameters.Clear();
                BookStock.Id = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
        }
        internal void UpdateBookStock(BookStock BookStock)
        {
            try
            {
                m_dataBaseConectivity.MySqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SQLScript.UpdateBookStock(), m_dataBaseConectivity.MySqlConnection);

                cmd.Parameters.Add("Code", MySqlDbType.VarChar).Value = BookStock.Code;
                cmd.Parameters.Add("Name", MySqlDbType.VarChar).Value = BookStock.Name;
                cmd.Parameters.Add("Remark", MySqlDbType.VarChar).Value = BookStock.Remark;
                cmd.Parameters.Add("ModifiedDate", MySqlDbType.Datetime).Value = DateTime.Now;
                cmd.Parameters.Add("ModifiedUserId", MySqlDbType.Int32).Value = BookStock.ModifiedUserId;
                cmd.Parameters.Add("BookId", MySqlDbType.Int32).Value = BookStock.BookId;
                cmd.Parameters.Add("BookBalance", MySqlDbType.Decimal).Value = BookStock.BookBalance;

                cmd.Parameters.Add("Id", MySqlDbType.Int32).Value = BookStock.Id;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.MySqlConnection.Close();
            }
        }
    }
}